package zut.cs.liziflower.controller;


import org.springframework.web.bind.annotation.*;
import zut.cs.liziflower.entity.Address;
import zut.cs.liziflower.entity.Result;
import zut.cs.liziflower.service.impl.AddressServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xuwenke
 * @since 2022-05-25
 */
@RestController
@RequestMapping("/api/address")
public class AddressController {
    @Resource
    private AddressServiceImpl addressService;

    @PostMapping("add-address")
    public Result<?> addAddress(@RequestParam("userid") Integer userid,
                                @RequestBody Address address) {
        //添加收货地址
        int i = addressService.addAddress(userid, address.getReceivephone(),
                address.getReceiceprovince(), address.getReceivecity(),
                address.getReceivedistrict(), address.getReceiveaddress());
        if (i == 0) {
            return Result.success("添加成功");
        }
        return Result.error("405", "添加失败", null);
    }

    @GetMapping("get-address")
    public Result<?> getAllAddress(@RequestParam("userid") Integer userid) {
        List<Address> list = addressService.getAllAddress(userid);
        if (list.isEmpty()) {
            return Result.error("405", "没找到", null);
        }
        return Result.success(list);
    }

    @DeleteMapping("delete-address")
    public Result<?> deleteAddress(@RequestParam("addressid") Integer addressid) {

        int i = addressService.deleteAddress(addressid);
        if (i == 1) {
            return Result.success("删除成功");
        }
        return Result.error("405", "删除失败", null);
    }

}
